package cn.mastercom.gui.hdfsExplorer.views;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.*;

import cn.mastercom.sssvr.util.ResourcesDepository;
import lombok.extern.log4j.Log4j;
import org.apache.log4j.Logger;

import cn.mastercom.sssvr.util.HdfsExplorerHelper;
import cn.mastercom.sssvr.util.HdfsExplorerHelper.GotoPathCmd;

@Log4j
public class ToolBar extends JToolBar implements Observer {

    private static final long serialVersionUID = 1L;


    private transient HdfsExplorerHelper helper;

    private transient ItemListener selectedItemListener = e->{
        if (e.getStateChange() == ItemEvent.SELECTED) {
            String keyWord = e.getItem().toString();
            helper.executeCmd(new GotoPathCmd(keyWord, helper));
        }
    };

    private JButton undo;
    private JButton redo;
    private JComboBox<String> searchBar;
    private JButton enter;
    private JButton refresh;
    private JLabel tip;

    /**
     * 这个对象交给{@link cn.mastercom.gui.hdfsExplorer.views.TablePane}处理，该类只是负责该对象的布局和样式，不更改该对象的值、状态或事件
     */
    private JTextField filterField;
    /**
     * 这个对象交给{@link cn.mastercom.gui.hdfsExplorer.views.TablePane}处理，该类只是负责该对象的布局和样式，不更改该对象的值、状态或事件
     */
    private JLabel filterDetail;

    public ToolBar(HdfsExplorerHelper helper) {

        this.helper = helper;
        helper.addObserver(this);

        initUI();
        initLookAndFeel();

        addEventListener();
        addActionListener();
    }

    public JTextField getFilterField() {
        return filterField;
    }

    public JLabel getFilterDetail() {
        return filterDetail;
    }

    private void initUI() {
        undo = new JButton(ResourcesDepository.getIcon("images/back.gif"));
        redo = new JButton(ResourcesDepository.getIcon("images/forward.gif"));
        searchBar = new JComboBox<>();
        searchBar.setEditable(true);
        searchBar.setPreferredSize(new Dimension(600, searchBar.getHeight()));
        enter = new JButton(ResourcesDepository.getIcon("images/jump_to.gif"));
        refresh = new JButton(ResourcesDepository.getIcon("images/refresh.gif"));
        tip = new JLabel("过滤");
        filterField = new JTextField();
        filterDetail = new JLabel();

        this.add(undo);
        this.add(redo);
        this.add(searchBar);
        this.add(enter);
        this.add(refresh);
        this.addSeparator();
        this.add(tip);
        this.add(filterField);
        this.add(filterDetail);
    }

    private void addEventListener() {
        undo.addActionListener(e -> helper.getCmdManager().undo());
        redo.addActionListener(e -> helper.getCmdManager().redo());
        searchBar.addItemListener(selectedItemListener);
        enter.addActionListener(e -> {
            String keyWord = searchBar.getEditor().getItem().toString();
            helper.executeCmd(new GotoPathCmd(keyWord, helper));
        });
        refresh.addActionListener(e -> helper.refresh());
    }

    private void addActionListener() {
    }

    private void initLookAndFeel() {
        helper.initLookAndFeel();
        this.setFloatable(false);
    }

    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof HdfsExplorerHelper
                && arg instanceof String) {
            // 移除选中事件，因为每次移除（插入）item时都有可能触发选中事件
            searchBar.removeItemListener(selectedItemListener);

            String value = (String) arg;

            DefaultComboBoxModel<String> model = (DefaultComboBoxModel<String>) searchBar.getModel();

            // 去掉重复的记录
            for (int i = model.getSize() - 1; i >= 0; i--) {
                if (model.getElementAt(i).equals(value)) {
                    model.removeElementAt(i);
                }
            }

            // 只保留最近15条记录
            while (searchBar.getItemCount() > 15) {
                searchBar.removeItemAt(searchBar.getItemCount() - 1);
            }

            model.insertElementAt(value, 0);
            searchBar.setSelectedItem(value);
            searchBar.addItemListener(selectedItemListener);
        }
    }
}
